package com.lucien.system.service.impl;

import com.lucien.common.dto.RegistGroupDto;
import com.lucien.common.fornt.SysGroupFornt;
import com.lucien.common.fornt.SysUserFornt;
import com.lucien.common.utils.StringUtils;
import com.lucien.system.domain.*;
import com.lucien.system.mapper.SysGroupMapper;
import com.lucien.system.mapper.SysGroupMemberMapper;
import com.lucien.system.service.SysGroupMemberService;
import com.lucien.system.service.SysGroupMessageService;
import com.lucien.system.service.SysGroupService;
import com.lucien.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysGroupServiceImpl implements SysGroupService {

    @Autowired
    private SysGroupMapper groupMapper;

    @Autowired
    private SysGroupMemberMapper memberMapper;

    @Autowired
    private SysGroupMemberService memberService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysGroupMessageService groupMessageService;

    /**
     * 创建群聊
     *
     * @param groupDto
     * @return
     */
    @Override
    public int createGroup(RegistGroupDto groupDto) {
        SysGroup sysGroup = new SysGroup();
        sysGroup.setName(groupDto.getGroupName());
        sysGroup.setCreator(groupDto.getCreator());
        sysGroup.setType(new Byte(String.valueOf(1)));
        sysGroup.setUsercnt(groupDto.getMember().size());
        sysGroup.setStatus(new Byte(String.valueOf(0)));
        sysGroup.setVersion(new Integer(1));
        sysGroup.setLastchated(new Date());
        int insert = groupMapper.insert(sysGroup);
        if (insert != 1) {
            return -1;
        }
        //获取群组ID
        Long groupId = selectByName(groupDto.getGroupName(), groupDto.getCreator()).getGroupId();
        boolean flag = true;

        // 添加member表
        List<Integer> users = groupDto.getMember();
        users.add(groupDto.getCreator());
        for (Integer userId : users) {
            SysGroupMember member = new SysGroupMember();
            member.setGroupid(groupId);
            member.setUserid(userId);
            member.setStatus(new Byte(String.valueOf(0)));
            if (memberMapper.insert(member) != 1) {
                flag = false;
                return -2;
            }
        }

        if (!flag) {
            delGroup(groupId);
        }
        return 1;
    }

    /**
     * 通过群名获取群信息
     *
     * @param groupName
     * @param creator
     * @return
     */
    @Override
    public SysGroupFornt selectByName(String groupName, Integer creator) {
        SysGroupExample example = new SysGroupExample();
        SysGroupExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(groupName);
        criteria.andCreatorEqualTo(creator);

        List<SysGroup> groups = groupMapper.selectByExample(example);
        if (groups.size() == 0) {
            return null;
        }

        SysGroup group = groups.get(0);
        List<SysGroupMember> members = memberService.selectByGroupId(group.getId());

        SysGroupFornt groupFornt = new SysGroupFornt();
        groupFornt.setGroupId(group.getId());
        groupFornt.setGroupName(group.getName());
        groupFornt.setAvatar(group.getAvatar());
        groupFornt.setCreator_id(group.getCreator());
        groupFornt.setCreator(userService.selectByUserId(group.getCreator()).getUsername());
        groupFornt.setUserCnt(members.size());
        List<SysUserFornt> userFornts = new ArrayList<>();
        for (SysGroupMember member : members) {
            SysUser user = userService.selectByUserId(member.getUserid());
            SysUserFornt userFornt = new SysUserFornt();
            userFornt.setUserId(user.getId());
            userFornt.setUsername(user.getUsername());
            userFornt.setUserProfile(user.getUserProfile());
            userFornt.setUserState(user.getUserStateId());
            userFornt.setGender(user.getGender());
            userFornts.add(userFornt);
        }
        groupFornt.setUserFornts(userFornts);

        return groupFornt;
    }

    /**
     * 通过groupId获取群聊信息
     * @param groupId
     * @return
     */
    @Override
    public SysGroupFornt selectByGroupId(Long groupId) {
        SysGroup group = groupMapper.selectByPrimaryKey(groupId);
        if (StringUtils.isNull(group)){
            return null;
        }
        List<SysGroupMember> members = memberService.selectByGroupId(groupId);

        SysGroupFornt groupFornt = new SysGroupFornt();
        groupFornt.setGroupId(group.getId());
        groupFornt.setGroupName(group.getName());
        groupFornt.setAvatar(group.getAvatar());
        groupFornt.setCreator_id(group.getCreator());
        groupFornt.setCreator(userService.selectByUserId(group.getCreator()).getUsername());
        groupFornt.setUserCnt(members.size());

        List<SysUserFornt> userFornts = new ArrayList<>();
        for (SysGroupMember member : members) {
            SysUser user = userService.selectByUserId(member.getUserid());
            SysUserFornt userFornt = new SysUserFornt();
            userFornt.setUserId(user.getId());
            userFornt.setUsername(user.getUsername());
            userFornt.setUserProfile(user.getUserProfile());
            userFornt.setUserState(user.getUserStateId());
            userFornt.setGender(user.getGender());
            userFornts.add(userFornt);
        }
        groupFornt.setUserFornts(userFornts);


        return groupFornt;
    }

    /**
     * 删除群聊
     *
     * @param groupId
     * @return
     */
    @Override
    public int delGroup(Long groupId) {
        if (groupMapper.deleteByPrimaryKey(groupId) != 1) {
            return -1;
        }

        SysGroupMemberExample example = new SysGroupMemberExample();
        SysGroupMemberExample.Criteria criteria = example.createCriteria();
        criteria.andGroupidEqualTo(groupId);

        List<SysGroupMember> members = memberMapper.selectByExample(example);
        for (SysGroupMember member : members) {
            if (memberMapper.deleteByPrimaryKey(member.getId()) != 1) {
                return -1;
            }
        }

        groupMessageService.delGroupMsg(groupId);

        return 1;
    }

    /**
     * 获取用户群聊列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<SysGroup> groupList(Integer userId) {
        Integer[] integers = memberService.selectByUserId(userId);
        if (integers.length == 0) {
            return new ArrayList<>();
        } else {
            List<SysGroup> groups = new ArrayList<>();
            for (int i = 0; i < integers.length; i++) {
                SysGroup sysGroup = groupMapper.selectByPrimaryKey(Long.valueOf(integers[i]));
                groups.add(sysGroup);
            }
            return groups;
        }
    }

    /**
     * 获取群聊成员信息
     * @param groupId
     * @return
     */
    @Override
    public List<SysUserFornt> getGroupMembers(Long groupId) {
        List<SysGroupMember> groupMembers = memberService.selectByGroupId(groupId);
        List<SysUserFornt> userFornts = new ArrayList<>();
        for (SysGroupMember member : groupMembers) {
            SysUser user = userService.selectByUserId(member.getUserid());
            SysUserFornt userFornt = new SysUserFornt();
            userFornt.setUserId(user.getId());
            userFornt.setUsername(user.getUsername());
            userFornt.setUserProfile(user.getUserProfile());
            userFornt.setGender(user.getGender());
            userFornt.setUserState(user.getUserStateId());

            userFornts.add(userFornt);
        }

        return userFornts;
    }

    /**
     * 通过群名获取群ID
     * @return
     */
    @Override
    public Integer getGroupIdByName(String groupName) {
        SysGroupExample example = new SysGroupExample();
        SysGroupExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(groupName);

        List<SysGroup> groups = groupMapper.selectByExample(example);

        return Math.toIntExact(groups.get(0).getId());
    }

}
